<template>
  <view class="order-container" :style="{height: `${windowHeight}px`}">
    <view class="header">
      <view class="nav-back" @click="goBack">
        <view class="iconfont icon-left"></view>
      </view>
      <view class="title">充值订单</view>
    </view>
    
    <view class="order-list" v-if="orderList.length > 0">
      <view class="order-item" v-for="(item, index) in orderList" :key="index" @click="goToDetail(item.id)">
        <view class="order-header">
          <view class="order-no">订单号：{{ item.orderNo }}</view>
          <view class="order-status" :class="{ 'status-paid': item.paymentCompletionTime }">
            {{ item.paymentCompletionTime ? '已支付' : '待支付' }}
          </view>
        </view>
        <view class="order-content">
          <view class="order-info">
            <view class="info-item">
              <text class="label">套餐：</text>
              <text class="value">{{ item.coupons.couponsInfo || '未知套餐' }}</text>
            </view>
            <view class="info-item">
              <text class="label">金额：</text>
              <text class="value price">¥{{ item.orderAmount/100 }}</text>
            </view>
            <view class="info-item">
              <text class="label">支付方式：</text>
              <text class="value">{{ item.paymentType === '0' ? '微信' : item.paymentType === '1' ? '支付宝' : '未知' }}</text>
            </view>
            <view class="info-item">
              <text class="label">支付时间：</text>
              <text class="value">{{ item.paymentCompletionTime || '未支付' }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    
    <view class="empty-container" v-else>
      <view class="iconfont icon-empty text-gray empty-icon"></view>
      <view class="empty-text">暂无订单记录</view>
    </view>
  </view>
</template>

<script>
  import { listOrder } from '@/api/products/order'
  
  export default {
    data() {
      return {
        orderList: [],
        loading: false,
        page: 1,
        total: 0,
        limit: 10
      }
    },
    computed: {
      windowHeight() {
        return uni.getSystemInfoSync().windowHeight
      }
    },
    onLoad() {
      this.getOrderList()
    },
    methods: {
      // 获取订单列表
      getOrderList() {
        this.loading = true
        const query = {
          pageNum: this.page,
          pageSize: this.limit
        }
        
        listOrder(query).then(res => {
          if (res.code === 200) {
            this.orderList = res.rows || []
            this.total = res.total
          } else {
            this.$modal.showToast(res.msg || '获取订单列表失败')
          }
        }).catch(err => {
          console.error('获取订单列表失败', err)
          this.$modal.showToast('获取订单列表失败')
        }).finally(() => {
          this.loading = false
        })
      },
      
      // 跳转到订单详情
      goToDetail(id) {
        this.$tab.navigateTo(`/pages/mine/order/detail?id=${id}`)
      },
      
      // 返回上一页
      goBack() {
        uni.navigateBack()
      }
    }
  }
</script>

<style lang="scss" scoped>
  page {
    background-color: #f5f6f7;
  }
  
  .order-container {
    width: 100%;
    
    .header {
      padding: 20px 15px;
      background-color: #3c96f3;
      display: flex;
      align-items: center;
      
      .nav-back {
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        
        .iconfont {
          color: #fff;
          font-size: 24px;
        }
      }
      
      .title {
        color: #fff;
        font-size: 18px;
        font-weight: bold;
        margin-left: 10px;
      }
    }
    
    .order-list {
      padding: 10px;
      
      .order-item {
        background-color: #fff;
        border-radius: 8px;
        margin-bottom: 15px;
        overflow: hidden;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        
        .order-header {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 12px 15px;
          border-bottom: 1px solid #f5f6f7;
          
          .order-no {
            font-size: 14px;
            color: #333;
          }
          
          .order-status {
            font-size: 14px;
            color: #ff9800;
            
            &.status-paid {
              color: #4caf50;
            }
          }
        }
        
        .order-content {
          padding: 15px;
          
          .order-info {
            .info-item {
              display: flex;
              margin-bottom: 8px;
              
              .label {
                color: #666;
                font-size: 14px;
                width: 80px;
              }
              
              .value {
                color: #333;
                font-size: 14px;
                flex: 1;
                
                &.price {
                  color: #ff5722;
                  font-weight: bold;
                }
              }
            }
          }
        }
      }
    }
    
    .empty-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding-top: 100px;
      
      .empty-icon {
        font-size: 80px;
        color: #d0d0d0;
      }
      
      .empty-text {
        margin-top: 15px;
        color: #999;
        font-size: 14px;
      }
    }
  }
</style> 